package com.zhoug.widget.bottomnavigation;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zhoug.common.utils.BitmapUtils;
import com.zhoug.widget.R;

import androidx.annotation.DrawableRes;
import androidx.annotation.Nullable;

/**
 * 底部导航菜单组件
 *
 * @Author 35574
 * @Date 2020/7/20
 * @Description
 */
public class BottomNavigationView extends LinearLayout {
    private static final String TAG = ">>>FooterNavigationView";
    private static final boolean DEBUG = true;
    /**
     * 文字
     */
    private String text;
    /**
     * 非选中时的图片
     */
    private @DrawableRes
    int iconNormal;
    /**
     * 选中时的图片
     */
    private @DrawableRes
    int iconSelected;

    /**
     * 是否选中
     */
    private boolean selected;

    /**
     * 图片和文字之间的间距
     */
    private int spaceTextAndIcon;

    /**
     * 文字大小
     */
    private float textSize;
    /**
     * 非选中时文字颜色
     */
    private int textColor;
    /**
     * 选中时文字颜色
     */
    private int selectedTextColor;

    private int badColor=1;//无效的颜色

    private ImageView mImageView;
    private TextView mTextView;


    public BottomNavigationView(Context context) {
        this(context, null);
    }

    public BottomNavigationView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BottomNavigationView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setOrientation(LinearLayout.VERTICAL);
        setGravity(Gravity.CENTER_HORIZONTAL);
        init(context, attrs, defStyleAttr);
        initView();

        setSelected(selected);
    }

    private void init(Context context, AttributeSet attrs, int defStyleAttr) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.BottomNavigationView, 0, defStyleAttr);
        text = a.getString(R.styleable.BottomNavigationView_android_text);
        iconNormal = a.getResourceId(R.styleable.BottomNavigationView_iconNormal, -1);
        iconSelected = a.getResourceId(R.styleable.BottomNavigationView_iconSelected, -1);
        selected = a.getBoolean(R.styleable.BottomNavigationView_selected, false);
        spaceTextAndIcon = a.getDimensionPixelOffset(R.styleable.BottomNavigationView_spaceTextAndIcon, 0);
        textSize = a.getDimension(R.styleable.BottomNavigationView_android_textSize, -1);
        textColor = a.getColor(R.styleable.BottomNavigationView_android_textColor, badColor);
        selectedTextColor = a.getColor(R.styleable.BottomNavigationView_selectedTextColor, badColor);
        a.recycle();

    }

    private void initView() {
        if (mImageView == null) {
            mImageView = new ImageView(getContext());
            LayoutParams params1 = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
            params1.weight = 1;
            mImageView.setLayoutParams(params1);
            mImageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
            addView(mImageView);
        }

        if (mTextView == null) {
            mTextView = new TextView(getContext());
            LayoutParams params2 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            params2.topMargin = spaceTextAndIcon;
            mTextView.setLayoutParams(params2);
            mTextView.setSingleLine();
            addView(mTextView);
        }
    }

    @Override
    public void setSelected(boolean selected) {
        this.selected = selected;
        setIcon();
        setText();
    }

    @Override
    public boolean isSelected() {
        return selected;
    }
    /**
     * 设置应该显示的图片
     */
    private void setIcon() {
        if (mImageView != null) {
            if (selected) {
                if (iconSelected != -1) {
                    mImageView.setImageResource(iconSelected);
                }
            } else {
                if (iconNormal != -1) {
                    mImageView.setImageResource(iconNormal);
                }
            }
        }
    }

    /**
     * 设置文字
     * {@link #text 是null时不显示}
     */
    private void setText() {
        if (mTextView != null) {
            if (selected) {
                if (selectedTextColor !=badColor) {
                    mTextView.setTextColor(selectedTextColor);
                }
            } else {
                if (textColor != badColor) {
                    mTextView.setTextColor(textColor);
                }
            }

            if (textSize != -1) {
                mTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
            }
            if (text != null) {
                mTextView.setText(text);
                mTextView.setVisibility(VISIBLE);
            } else {
                mTextView.setText("");
                mTextView.setVisibility(GONE);
            }
        }
    }


}
